package Question5;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * Created by lenovo on 2017/3/7.
 */
public class Question5 {

    public static int jodgeSame(String str){
        Set<Character> sets = new HashSet<>();

        String shengXu = "0123456789";
        String jiangXu = "9876543210";
        char[] chars = str.toCharArray();

        //计数数组初始化
        int[] count = new int[10];
        for(int i=0;i<10;i++){
            count[i]=0;
        }

        for(int i=0;i<chars.length;i++){
            sets.add(chars[i]);
        }
        int sum =0;
        if(sets.size() == 1){
            sum+=8;
        }else if(sets.size() == 2){
            if(str.substring(0,2).equals(str.substring(2,4))){
                sum+=1;
            }else if(chars[0]==chars[1]&&chars[2]==chars[3]){
                sum+=1;
            }else if(chars[0]==chars[1]&&chars[1]==chars[2] || chars[1]==chars[2]&&chars[2]==chars[3]){
                sum+=3;
            }
        }else if(sets.size() == 4){
            if(jiangXu.indexOf(str)!=-1||shengXu.indexOf(str)!=-1){
                sum+=5;
            }
        }

        //对该字符串出现的数字进行计数
        for(int i=0;i<chars.length;i++){
            count[chars[i]-'0']+=1;
        }
        return sum+=count[6]+count[8]+count[9];
    }


    public  static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();

        String str;
        while(n-->0){
            str = scanner.nextLine();
            System.out.println(Question5.jodgeSame(str));
        }

    }
}
